package com.ruoyi.lifecycle.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.mybatis.core.page.PageQuery;
import com.ruoyi.common.mybatis.core.page.TableDataInfo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.lifecycle.domain.bo.TMileStoneBo;
import com.ruoyi.lifecycle.domain.vo.TMileStoneVo;
import com.ruoyi.lifecycle.domain.vo.TimeLineEvenVo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.lifecycle.domain.bo.TFundBo;
import com.ruoyi.lifecycle.domain.vo.TFundVo;
import com.ruoyi.lifecycle.domain.TFund;
import com.ruoyi.lifecycle.mapper.TFundMapper;
import com.ruoyi.lifecycle.service.ITFundService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 基金信息Service业务层处理
 *
 * @author chq
 * @date 2025-06-06
 */
@RequiredArgsConstructor
@Service
public class TFundServiceImpl implements ITFundService {

    private final TFundMapper baseMapper;

    /**
     * 查询基金信息
     */
    @Override
    public TFundVo queryById(String id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询基金信息列表
     */
    @Override
    public TableDataInfo<TFundVo> queryPageList(TFundBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<TFund> lqw = buildQueryWrapper(bo);
        Page<TFundVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询基金信息列表
     */
    @Override
    public List<TFundVo> queryList(TFundBo bo) {
        LambdaQueryWrapper<TFund> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<TFund> buildQueryWrapper(TFundBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<TFund> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getFundcode()), TFund::getFundcode, bo.getFundcode());
        lqw.like(StringUtils.isNotBlank(bo.getFundname()), TFund::getFundname, bo.getFundname());
        lqw.like(StringUtils.isNotBlank(bo.getFundengname()), TFund::getFundengname, bo.getFundengname());
        lqw.like(StringUtils.isNotBlank(bo.getFundenglishname()), TFund::getFundenglishname, bo.getFundenglishname());
        lqw.eq(StringUtils.isNotBlank(bo.getFundtype()), TFund::getFundtype, bo.getFundtype());
        lqw.eq(StringUtils.isNotBlank(bo.getRisklevel()), TFund::getRisklevel, bo.getRisklevel());
        lqw.eq(StringUtils.isNotBlank(bo.getFeetixtype()), TFund::getFeetixtype, bo.getFeetixtype());
        lqw.eq(StringUtils.isNotBlank(bo.getClassfund()), TFund::getClassfund, bo.getClassfund());
        lqw.eq(StringUtils.isNotBlank(bo.getPfundcode()), TFund::getPfundcode, bo.getPfundcode());
        lqw.eq(bo.getSubamountlast() != null, TFund::getSubamountlast, bo.getSubamountlast());
        lqw.orderByDesc(TFund::getFundcode);
        return lqw;
    }

    /**
     * 新增基金信息
     */
    @Override
    public Boolean insertByBo(TFundBo bo) {
        TFund add = BeanUtil.toBean(bo, TFund.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改基金信息
     */
    @Override
    public Boolean updateByBo(TFundBo bo) {
        TFund update = BeanUtil.toBean(bo, TFund.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(TFund entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除基金信息
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public List<TimeLineEvenVo> getMileStoneInfos(TMileStoneBo bo) {
        return baseMapper.getMileStoneInfos(bo);
    }

}
